package Service.SurroundService;

import Factory.ParamChangeUtil;
import Model.IntermittentCommunicationModel.BaseShip;
import Model.IntermittentCommunicationModel.ShipController;
import Service.ShipCalculate;

public class IntermittentCommunicationService extends ShipCalculate {

    ParamChangeUtil paramChangeUtil = new ParamChangeUtil(); // 数据转换器

    public static String name = "间歇通信组件";

    static int SleepTime; // 计算休眠间隔
    static volatile boolean isWorking = false; // 是否运行

    // 计算实例，包含计算信息
    public ShipController shipController; // 目标队列

    /**
     * 转换示例
     *
     * @param sleepTime 睡眠时间
     */
    public IntermittentCommunicationService(int sleepTime) {
        this.SleepTime = sleepTime;
        this.isWorking = true;
    }

    public void Init() {
        System.out.println(GetName() + "初始化了");
        this.initTarget();
        this.initShip();

        // 保存数据
        saveData(0);
    }

    public void Calculate() {
        this.calculate();
    }

    public boolean GetWorkingState() {
        return this.isWorking;
    }

    public void ShutDown() {
        this.isWorking = false;
    }

    public String GetName() {
        return name;
    }

    public void SetCalSpeed(int sleepTime){ this.SleepTime = sleepTime;};

    // 初始化目标信息
    public void initTarget() {

    }

    // 初始化智能体
    public void initShip() {
        shipController.initShipsData();
    }

    /**
     * 保存数据
     *
     * @param time 时间
     */
    public void saveData(double time) {
        //保存目标队列当前位置
        paramChangeUtil.SaveAllData(this, time);
    }

    // 计算并更新需要更新的数据
    public void calculate() {
        isWorking = true;

        double timeLength = 0.001;
        BaseShip leaderShip = ShipController.baseShips.get(0);
        for (int times = 0; times <= 5000000; times++) {

            if(!isWorking)
            {
                break;
            }

            // 休眠
            try {
                Thread.sleep(SleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("times" + times*timeLength + "领航船位置：" + ShipController.baseShips.get(0).ηMatrix);
            System.out.println("times" + times*timeLength + "领航船速度：" + ShipController.baseShips.get(0).getTansPositionSpeed());

            for (int i = 1; i < ShipController.baseShips.size(); i++) {
                BaseShip nowShip = ShipController.baseShips.get(i);
//                SimpleMatrix control = ShipController.getControlMatrix(i);
                // 设置船的控制矩阵
//                nowShip.setControlMatrix(control);
                // 设置船的加速度矩阵
                nowShip.setAccelerateSpeed(
                        nowShip.ηstMatrix.plus(ShipController.calculateAccelerateSpeed(i)
                                .scale(0.001)
                        )
                );
                // 设置船的大地坐标系下的速度 = 原速 + 加速度 * 时间长
                nowShip.setSpeedWithMatrix(
                        nowShip.ηsMatrix.plus(
                                ShipController.calculateAccelerateSpeed(i).scale(timeLength)
                        )
                );
                // 设置船的位置 = 原位置 + 原速度*时间长
                nowShip.setPosition(
                        nowShip.ηMatrix.plus(
                                nowShip.ηsMatrix.scale(timeLength) )
                );
                System.out.println("times" + times*timeLength + "船 " + i + "的位置：" + nowShip.ηMatrix);
                System.out.println("times" + times*timeLength + "船 " + i + "的速度：" + nowShip.ηsMatrix);
                System.out.println("times" + times*timeLength + "船 " + i + "的加速度：" + nowShip.ηstMatrix);
            }

            // 领航船移动
            ShipController.setTime(ShipController.nowTime + timeLength); // 时间变化
            leaderShip.LeaderMove();
//            leaderShip.setPosition(leaderShip.getLeaderPosition(ShipController.nowTime)); // 设置新位置

            // 保存数据
            saveData(times);

        }

//        double t0 = 0.001;
//        double timeLength = 0.001;
//        double saveTime = 0;            //保存时间
//        while (!ship.circularFinish(timeLength, ship, target) && isWorking) {
////            System.out.println("一阶动态包围计算中");
//
//            try {
//                Thread.sleep(SleepTime);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//            //让目标移动，改变目标的x,y，并求出新的目标中心，计算新的di
//            target.setPosition(t0);
//            target.setCenter();
//            target.setd(timeLength, ship, target);
//
//            //通过控制器去修改智能体对于目标的各项数值
//            ship.circularCotroller(timeLength, target);
//            ship.sety(timeLength);
//

//
//            //时间跳到下一时刻
//            t0 += timeLength;
//            saveTime++;
//        }

        isWorking = false;
    }
}
